package com.rech.xbexpress3;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.rech.utils.OtherUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 选择寄件方式页
 * Created by sunshine on 2020/9/29
 */
public class ChoosePostWayActivity extends AppCompatActivity {
    private Button post_immediately;
    private Button post_wx;
    private Button post_cainiao;
    private Timer countTimer;
    private TextView djs;
    private int jjcount = 100;

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sharedPreferences = getSharedPreferences("xbbox", Context.MODE_PRIVATE);
        String orientation = sharedPreferences.getString("Orientation", "");
        if (orientation.equals("landscape")) {
            setContentView(R.layout.activity_choose_postway_land);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setContentView(R.layout.activity_choose_postway);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        //eventbus
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        getSupportActionBar().hide();

        djs = findViewById(R.id.djs);

        //统一清一次寄件信息
        SharedPreferences.Editor editor = getSharedPreferences("xbbox",MODE_PRIVATE).edit();
        editor.putString("jj_sjr_address","");
        editor.putString("jj_jjr_address","");
        editor.putString("kdy_id","");
        editor.putString("wl_id","");
        editor.putString("jj_kdysj","");
        editor.putString("jj_ljm1","");
        //editor.putString("jj_ljm2","");
        editor.putString("jj_sjr","");
        editor.apply();

        /*post_immediately = (Button) findViewById(R.id.post_immediately);
        post_immediately.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int mExpType = 0;
                SharedPreferences.Editor editor = getSharedPreferences("xbbox",MODE_PRIVATE).edit();
                editor.putInt("jj_exp",mExpType);
                //editor.putString("jj_boxvalue",boxValue);
                editor.apply();
                Intent intent = new Intent();
                intent.putExtra("isFromWX", false);
                setResult(1002, intent);
                Exit();
                finish();
            }
        });*/
        post_wx = (Button) findViewById(R.id.wx_post);
        post_wx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int mExpType = 0;
                SharedPreferences.Editor editor = getSharedPreferences("xbbox",MODE_PRIVATE).edit();
                editor.putInt("jj_exp",mExpType);
                //editor.putString("jj_boxvalue",boxValue);
                editor.apply();
                Intent intent = new Intent();
                setResult(1003, intent);
                Exit();
                finish();
            }
        });
        post_cainiao = (Button) findViewById(R.id.cainiao_post);
        post_cainiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int mExpType = 2;
                SharedPreferences.Editor editor = getSharedPreferences("xbbox",MODE_PRIVATE).edit();
                editor.putInt("jj_exp",mExpType);
                //editor.putString("jj_boxvalue",boxValue);
                editor.apply();

                Intent intent = new Intent();
                intent.putExtra("isFromCainiao", true);
                setResult(1004, intent);
                Exit();
                finish();
            }
        });

        countTimer=new Timer();
        countTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                jjcount--;
                Message message = new Message();
                message.what=20000;
                EventBus.getDefault().post(message);
                if(jjcount < 1){
                    //OtherUtil.restartApplication(Jj2Activity.this);
                    Exit();
                    finish();
                }
            }
        },0,1000);

        //OtherUtil.OpenVoice1(this);

        TextView tv_version = findViewById(R.id.tv_version);
        TextView tv_waiter = findViewById(R.id.tv_waiter);
        try {
            tv_version.setText("v" + OtherUtil.getVersion(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String phone = sharedPreferences.getString("CabinetType_phone","");
        tv_waiter.setText("客服电话:" + phone);

        TextView tv_back = findViewById(R.id.tv_back);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Exit();
                finish();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN,priority = 100,sticky = false)     //在UI线程执行，100  为优先级
    public void onEvent(Message message){
        Intent intent = new Intent();
        switch(message.what){
            case 10402:
                //handleKDLjmTask(message.obj);
                break;
            case 10403:
                //Toast.makeText(Kd3Activity.this, "揽件码错误", Toast.LENGTH_SHORT).show();
                break;
            case 20000:
                handleDJS();
                break;
        }
    }

    private void handleDJS(){
        djs.setText(String.valueOf(jjcount));
    }

    private void Exit(){
        countTimer.cancel();
        EventBus.getDefault().unregister(this);
    }
}
